package com.viewpagerindicator.sample.beans;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhao on 2015/11/7.
 */
public class Goods {
    private int id;
    private String name;
    private String remark;
    private String thumb;
    private String desc;
    private String createAt;
    private int typeId;
    private BigDecimal price;
    private boolean isBuyed=false;
    private List<JSONObject> items;

    public List<JSONObject> getItems() {
        return items;
    }

    public void setItems(List<JSONObject> items) {
        this.items = items;
    }

    public int getTypeId() {
        return typeId;
    }

    public void setTypeId(int typeId) {
        this.typeId = typeId;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getCreateAt() {
        return createAt;
    }

    public void setCreateAt(String createAt) {
        this.createAt = createAt;
    }

    public String getThumb() {
        return thumb;
    }

    public void setThumb(String thumb) {
        this.thumb = thumb;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public boolean isBuyed() {
        return isBuyed;
    }

    public void setIsBuyed(boolean isBuyed) {
        this.isBuyed = isBuyed;
    }

    public static List<Goods> parseList(byte[] responseBody) throws JSONException {
        JSONTokener jsonParser = new JSONTokener(new String(responseBody));
        // 此时还未读取任何json文本，直接读取就是一个JSONObject对象。
        // 如果此时的读取位置在"name" : 了，那么nextValue就是"yuanzhifei89"（String）
        List<Goods> goodsList=new ArrayList<Goods>();
        JSONArray person = (JSONArray) jsonParser.nextValue();
        // 接下来的就是JSON对象的操作了
        if(person.length()>0){
            for(int i=0;i<person.length();i++){
                JSONObject jsonObject=person.getJSONObject(i);
                Goods goods=new Goods();
                goods.setId(jsonObject.getInt("id"));
                goods.setName(jsonObject.getString("name"));
                goods.setDesc(jsonObject.getString("desc"));
                goods.setRemark(jsonObject.getString("remark"));
                goods.setRemark(jsonObject.getString("remark"));
                goods.setThumb(jsonObject.getString("thumb"));
                goods.setCreateAt(jsonObject.getString("create_at"));
                goods.setPrice(new BigDecimal(jsonObject.getString("price")));
                if(jsonObject.has("isBuyed")){
                    goods.setIsBuyed(jsonObject.getBoolean("isBuyed"));
                }
                goodsList.add(goods);
            }
        }
        return goodsList;
    }
    public static Goods parseWithItem(byte[] responseBody) throws JSONException {
        JSONTokener jsonParser = new JSONTokener(new String(responseBody));
        // 此时还未读取任何json文本，直接读取就是一个JSONObject对象。
        // 如果此时的读取位置在"name" : 了，那么nextValue就是"yuanzhifei89"（String）
        List<JSONObject> goodsList=new ArrayList<JSONObject>();
        JSONObject jsonObject = (JSONObject) jsonParser.nextValue();
        Goods goods=new Goods();
        goods.setId(jsonObject.getInt("id"));
        goods.setName(jsonObject.getString("name"));
        goods.setDesc(jsonObject.getString("desc"));
        goods.setRemark(jsonObject.getString("remark"));
        goods.setThumb(jsonObject.getString("thumb"));
        goods.setCreateAt(jsonObject.getString("create_at"));
        goods.setPrice(new BigDecimal(jsonObject.getString("price")));
        JSONArray itemJsonarray=jsonObject.getJSONArray("items");
        if(jsonObject.has("isBuyed")){
            goods.setIsBuyed(jsonObject.getBoolean("isBuyed"));
        }

        // 接下来的就是JSON对象的操作了
        if(itemJsonarray.length()>0){
            for(int i=0;i<itemJsonarray.length();i++){
                JSONObject itemJsonObject=itemJsonarray.getJSONObject(i);
                goodsList.add(itemJsonObject);
            }
        }
        goods.setItems(goodsList);
        return goods;
    }
}
